#!/usr/bin/perl

use warnings;
use strict;

use DBI;
use Getopt::Long;
use Data::Dumper;


## get progran argv
my %data=(
	"masterport" => "3306",
    "slaveport" => "3306"
);
Getopt::Long::Configure();
GetOptions(\%data,
			'masterhost=s',
			'masterport=s',
			'masteruser=s',
			'masterpasswd=s',
			'slavehost=s',
			'slaveport=s',
			'slaveuser=s',
			'slavepasswd=s',
			'repluser=s',
			'replpasswd=s',
			'debug=s',
			'database=s',
			'help'
			);

#print $data{debug};
#print $abc{ttt};

if ( defined $data{help} ) {
    print <<FOO;
	$0: Auto replication master slave script

	 --slavehost    <host>     - MySQL instance running as a slave server
	 --slaveport    <d>   - port for the slave
	 --slaveuser    <user>    - Username with File/Process/Super privs
	 --slavepass    <pass>    - Password for above user
	 --masterhost   <host>    - MySQL instance running as server (override)
	 --masterport   <d>  - port for the master (override)
	 --masteruser   <user>   - Username for master (override)
	 --masterpass   <pass>   - Password for master
	 --repluser     <user>
	 --replpass	    <pass>
	 --database     <db>       db name 

FOO

exit;

}

## lock table and remember master status
print "Connecting to master $data{masterhost}:$data{masterport} as user $data{masteruser} \n" if $data{debug};
my $mdbh = DBI->connect("DBI:mysql:database=;host=$data{masterhost};port=$data{masterport}",$data{masteruser},$data{masterpasswd});
print "Cannot connect to master $data{masterhost}:$data{masterport} as user $data{masterpasswd} \n" if not $mdbh;
#lock table
my $sql = 'FLUSH TABLES WITH READ LOCK';
my $msth=$mdbh->prepare($sql);
print "cannot prepare $sql\n" if not $msth;
my $mres=$msth->execute;

$sql = 'SHOW MASTER STATUS';
$msth=$mdbh->prepare($sql);
print "cannot prepare $sql\n" if not $msth;
$mres=$msth->execute;

my $master_data;
my $mref;
my $ttt;
$ttt=$msth->fetchrow_hashref();
while ( $mref = $msth->fetchrow_hashref ) {
	        $master_data->{ $mref->{'Variable_name'}}  = $mref->{'Value'};
#	%ttt=($mref->{'Variable_name'},$mref->{'Value'});
}


## dump the master sql
my $datafile="/tmp/data.sql";
my $masterdump;
$masterdump=`mysqldump --skip-comments --databases $data{database} -h$data{masterhost} -u$data{masteruser} -p$data{masterpasswd} > $datafile`;
print $masterdump;
#$masterdump= system(date);

$sql = 'UNLOCK TABLES';
$msth=$mdbh->prepare($sql);
print "cannot prepare $sql\n" if not $msth;
$mres=$msth->execute;

## copy sql file to slave and insert sql
print "Connecting to slave $data{slavehost}:$data{slaveport} as user $data{slaveuser} \n" if $data{debug};
my $sdbh = DBI->connect("DBI:mysql:database=;host=$data{slavehost};port=$data{slaveport}",$data{slaveuser},$data{slavepasswd});
print "Cannot connect to slave $data{slavehost}:$data{slaveport} as user $data{slavepasswd} \n" if not $sdbh;
#
#$masterdump =~ s/;//g;
#$sql = $masterdump;
#my $ssth=$sdbh->prepare($sql);
#print "cannot prepare $sql\n" if not $ssth;
#my $sres=$ssth->execute;
print "mysql -h$data{slavehost} -u$data{slaveuser} -p$data{slavepasswd} < $datafile \n";
my $aa=`mysql -h$data{slavehost} -u$data{slaveuser} -p$data{slavepasswd} < $datafile`;



$sql = 'STOP SLAVE';
my $ssth=$sdbh->prepare($sql);
print "cannot prepare $sql\n" if not $ssth;
my $sres=$ssth->execute;

## change master and start slave
#$sql = "CHANGE MASTER TO MASTER_HOST='?',MASTER_USER='?',MASTER_PASSWORD='?',MASTER_LOG_FILE='?',MASTER_LOG_POS=$ttt->{Position}";
#$sql = 'CHANGE MASTER TO MASTER_HOST="?",MASTER_USER="?",MASTER_PASSWORD="?",MASTER_LOG_FILE="?",MASTER_LOG_POS=$ttt->{Position}';
$sql = "
	CHANGE MASTER TO 
	MASTER_HOST='$data{masterhost}',
	MASTER_USER='$data{masteruser}',
	MASTER_PASSWORD='$data{masterpasswd}',
	MASTER_LOG_FILE='$ttt->{File}',
	MASTER_LOG_POS=$ttt->{Position}
	";

print $sql ."\n";

$ssth=$sdbh->prepare($sql);
print "cannot prepare $sql\n" if not $ssth;
#print "$data{masterhost},$data{repluser},$data{replpasswd},$ttt->{File},$ttt->{Position} \n";
#my $sres=$ssth->execute($data{masterhost},$data{repluser},$data{replpasswd},$ttt->{File}) or die "Couldn't execute statement: $DBI::errstr; stopped";
#my $sres=$ssth->execute($data{masterhost},$data{repluser},$data{replpasswd},$ttt->{File},$ttt->{Position}+1-1) or die "Couldn't execute statement: $DBI::errstr; stopped";
$sres=$ssth->execute() or die "Couldn't execute statement: $DBI::errstr; stopped";

$sql = 'START SLAVE';
$ssth=$sdbh->prepare($sql);
print "cannot prepare $sql\n" if not $ssth;
$sres=$ssth->execute;


